//
//  PhotoAlbumController.m
//  soccer
//
//  Created by 上官千城 on 15/9/11.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OPhotoAlbumController.h"

@interface OPhotoAlbumController ()

@end

@implementation OPhotoAlbumController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    //底层scorllView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH , HEIGHT)];
    self.scrollView.delegate = self;
    //设置滚动范围
    self.scrollView.contentSize = CGSizeMake(WIDTH * 3, 0);
    //设置初始偏移量
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    //横向滚动条不可见
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //按页滚动
    self.scrollView.pagingEnabled = YES;
    
    [self.view addSubview:self.scrollView];
    
    
    
    // 可以根据数组里元素的数量来 循环
    for (int index = 0; index < 3; index++)
    {
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width * index, 0, self.view.frame.size.width, self.view.frame.size.height)];
        scrollView.minimumZoomScale = 0.5;
        scrollView.maximumZoomScale = 1.5;
        scrollView.delegate = self;
        [self.scrollView addSubview:scrollView];
        
        UIImageView * imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//        NSURL *url = [NSURL URLWithString:self.con.image];
//        [imageView sd_setImageWithURL:url];
        imageView.userInteractionEnabled = YES;
        //调整 视图的边距 和 适应模式
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.autoresizesSubviews = YES;
        imageView.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        
        [scrollView addSubview:imageView];
    }
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.center.x - 75 , HEIGHT - 170, 150, 30)];
//    self.pageControl.backgroundColor = [UIColor orangeColor];
    self.pageControl.numberOfPages = 3;
    [self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    self.pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self.view addSubview:self.pageControl];
    
    
    
    
    
    
    self.photo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
//    NSURL *url = [NSURL URLWithString:self.con.image];
//    [self.photo sd_setImageWithURL:url];
    //调整 视图的边距 和 适应模式
    self.photo.contentMode = UIViewContentModeScaleAspectFit;
    self.photo.autoresizesSubviews = YES;
    self.photo.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
//    [self.scrollView addSubview:self.photo];
    
    
}

//UIPageControl类型对象中调用的方法
- (void)changePage:(UIPageControl *)page
{
    [self.scrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.scrollView.frame.size.width, 0) animated:YES];
}

//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /*
     //注释部分为滑动一半的时候就改变页码值,用户体验更好
     if (scrollView == self.scroll) {
     int halfX = scrollView.frame.size.width / 2;
     self.pageControl.currentPage = (scrollView.contentOffset.x - self.scroll.frame.size.width - halfX) / (scrollView.frame.size.width) + 1;
     }
     */
    self.pageControl.currentPage = (scrollView.contentOffset.x - self.scrollView.frame.size.width) / scrollView.frame.size.width;
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.scrollView == scrollView) {
        
        //每次减速结束都让scollView所有的scrollView子视图缩放变为1
        for (UIScrollView * scrollv in scrollView.subviews) {
            if ([scrollv isKindOfClass:[UIScrollView class]]) {
                scrollv.zoomScale = 1.0;
            }
        }
        
        
        if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * 2) {
            
            NSLog(@"最后一页");//循环滚动
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
        }
        
        if (self.scrollView.contentOffset.x <= 0) {
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * 1, 0) animated:NO];
        }
        
    }
}

//
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    // 只有小scrollView才允许缩放
    if (scrollView != self.scrollView){
        //打印会发现，scrollview的子视图上第一个视图，一定是当前显示的视图
        NSLog(@"[scrollView.subviews firstObject] = %@",[scrollView.subviews objectAtIndex:0]);
        
        return [scrollView.subviews objectAtIndex:0];
        
    }else{
        
        return nil;
    }
    
}

//缩小后 图片显示在中间 这个小scrollview里我只做捏合后显示在中心点位置的逻辑
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    
    UIImageView *imageView = [scrollView.subviews firstObject];
    imageView.center = CGPointMake(scrollView.frame.size.width/2, scrollView.frame.size.height/2);
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
